﻿$(function () {
    // 获取当前时间----开始
    var myDate = new Date();	//创建Date对象
    var Y = myDate.getFullYear();   //获取当前完整年份
    var M = myDate.getMonth() + 1;  //获取当前月份
    var D = myDate.getDate();   //获取当前日1-31
    var H = myDate.getHours();  //获取当前小时
    var i = myDate.getMinutes();    //获取当前分钟
    var s = myDate.getSeconds();    //获取当前秒数
    // 月份不足10补0
    if(M < 10){
        M = '0' + M;
    }
    // 日不足10补0
    if(D < 10){
        D = '0' + D;
    }
    // 小时不足10补0
    if(H < 10){
        H = '0' + H;
    }
    // 分钟不足10补0
    if(i < 10){
        i = '0' + i;
    }
    // 秒数不足10补0
    if(s < 10){
        s = '0' +s;
    }
    // 拼接日期分隔符根据自己的需要来修改
    var nowDate = Y+'-'+M+'-'+D+' '+H+':'+i+':'+s;
    // 获取当前时间----结束

    // 初始化表格-----开始
    var tabledata = {};
    var workbook = new GC.Spread.Sheets.Workbook(document.getElementById("ss"),{
        // sheetCount:1,
        // allowUserDragDrop:false,
        // allowUserDragFill:false,
        // allowUserResize:false,
        // scrollbarMaxAlign:true

    });
    $.ajaxSettings.async = false;
    $.getJSON('/3.ssjson', function(json) {
        workbook.fromJSON(json);
    });
    $.ajaxSettings.async = true;
    //workbook配置信息
    workbook.options.allowUserDragDrop = false;
    workbook.options.allowUserResize = false;
    workbook.options.allowUserDragFill = false;
    workbook.options.scrollbarMaxAlign = true;
    // workbook.options.resizeZeroIndicator = 0;//行高列宽为0时显示单线还是双线

    function getData(){
        $.ajax({
            type : "post",
            url : "/WeightBridge/ccrb",
            data:{"createtime":nowDate},
            dataType : "json",
            async: false,
            success : function(data) {
                if(data.result!=="fail"){
                    console.log(data.result);
                    tabledata = data.result;
                }
            }
        });
        return tabledata;
    }
    getData();
    // 初始化表格-----结束

    // 绑定数据源与单元格修改事件-----开始
    var source = new GC.Spread.Sheets.Bindings.CellBindingSource(tabledata);
    var sheet = workbook.sheets[0];
    // sheet.defaults.rowHeight = 26;//修改默认单元格高度
    // sheet.defaults.colWeight = 80;//修改默认单元格宽度
    // sheet.autoFitColumn(0);//单元格内容自动行拓展
    // sheet.autoFitRow(0);//单元格内容自动列拓展
    sheet.setRowCount(30);             //设置行数
    sheet.setBindingPath(3, 5, 'cc_sum');
    sheet.setBindingPath(4, 5, 'dgy_sum');
    sheet.setBindingPath(5, 5, 'hgy_sum');
    sheet.setBindingPath(6, 5, 'wn_jk_sum');
    sheet.setBindingPath(7, 5, 'wn_qt_sum');
    sheet.setBindingPath(9, 5, 'e_sum');
    sheet.setBindingPath(10, 5, 'zls_cq_sum');
    sheet.setBindingPath(11, 5, 'zls_zb_sum');
    sheet.setBindingPath(12, 5, 'xnj_jk_sum');
    sheet.setBindingPath(13, 5, 'xnj_zt_sum');
    sheet.setDataSource(source);
    sheet.bind(GC.Spread.Sheets.Events.ValueChanged, function () {


        var updatedata = sheet.getDataSource().xf
        console.log(updatedata);
        var url ="";
        if(tabledata.createtime!==""&&tabledata.createtime!==undefined){
            url = "/WeightBridge/ccrb_update";
        }
        else{
            url = "/WeightBridge/ccrb_insert";
        }
        updatedata.createtime = nowDate;
        $.ajax({
            type : "post",
            url : url,
            data:{"cc_sum":updatedata.cc_sum,
                "dgy_sum":updatedata.dgy_sum,
                "createtime":nowDate,
                "hgy_sum":updatedata.hgy_sum,
                "wn_jk_sum":updatedata.wn_jk_sum,
                "wn_qt_sum":updatedata.wn_qt_sum,
                "zls_cq_sum":updatedata.zls_cq_sum,
                "zls_zb_sum":updatedata.zls_zb_sum,
                "xnj_jk_sum":updatedata.xnj_jk_sum,
                "xnj_zt_sum":updatedata.xnj_zt_sum,
                "e_sum":updatedata.e_sum},

            dataType : "json",
            success : function(data) {

            }
        });
        console.log(sheet.getDataSource().xf);
    });
    // 绑定数据源与单元格修改事件-----结束



    // 设置可编辑单元格样式----开始
    // var style1 = new GC.Spread.Sheets.Style();
    // style1.locked = false;
    // var style2 = new GC.Spread.Sheets.Style();
    // style2.locked = false;
    // style2.hAlign = GC.Spread.Sheets.HorizontalAlign.center;
    // style2.vAlign = GC.Spread.Sheets.VerticalAlign.center;
    // style2.borderLeft = new GC.Spread.Sheets.LineBorder("Black",GC.Spread.Sheets.LineStyle.thin);
    // style2.borderTop = new GC.Spread.Sheets.LineBorder("Black",GC.Spread.Sheets.LineStyle.thin);
    // style2.borderRight = new GC.Spread.Sheets.LineBorder("Black",GC.Spread.Sheets.LineStyle.thin);
    // style2.borderBottom = new GC.Spread.Sheets.LineBorder("Black",GC.Spread.Sheets.LineStyle.thin);
    // style2.font = 'small-caps 12pt/14pt "Times New Roman",Georgia,Serif';
    // 设置可编辑单元格样式----结束
    // 设置可编辑单元格----开始
    // sheet.setStyle(1, 1, style1);
    // sheet.setStyle(1, 5, style1);
    // sheet.setStyle(4, 5, style2);
    // var option = {
    //     allowSelectLockedCells:true,
    //     allowSelectUnlockedCells:true,
    //     allowFilter: true,
    //     allowSort: false,
    //     allowResizeRows: true,
    //     allowResizeColumns: false,
    //     allowEditObjects: false,
    //     allowDragInsertRows: false,
    //     allowDragInsertColumns: false,
    //     allowInsertRows: false,
    //     allowInsertColumns: false,
    //     allowDeleteRows: false,
    //     allowDeleteColumns: false,
    //     allowOutlineColumns: false,
    //     allowOutlineRows: false
    // };
    // sheet.options.protectionOptions = option;
    // sheet.options.isProtected = true;
    // 设置可编辑单元格----结束




    // var spread = new GC.Spread.Sheets.Workbook(document.getElementById('ss'),{sheetCount:1});
    var excelIo = new GC.Spread.Excel.IO();
    // var designer = new GC.Spread.Sheets.Designer.Designer(document.getElementById("designerHost"));
    // designer.setConfiguration(GC.Spread.Sheets.Designer.Toolbar);
    // 导出设置
    let exhortations = {
        includeBindingSource: true,  //导出是否包含绑定数据
        ignoreStyle: false,//是否忽略样式
        ignoreFormula: false,//是否忽略公式
        rowHeadersAsFrozenColumns:false,    //行头作为冻结列
        ColumnHeadersAsFrozenRows:false,   //列头作为冻结行
        includeAutoMergedCells: true      //自动合并相同单元格内容
    };
    //导出程序
    document.getElementById("exportexcel").onclick = function(){
        excelIo.save(workbook.toJSON(exhortations),function(blob) {
            saveAs(blob,'导出.xlsx');
        },function (e) {
            console.log(e)
        })
    }

    //导入程序
    // document.getElementById("upload").onclick = function(){
    //     let file = document.getElementById("file").files[0]
    //     excelIo.open(file,function(json) {
    //         workbook.fromJSON(json)
    //     },function (e) {
    //         console.log(e)
    //     })
    // }

})